<template>
  <div>
    <el-page-header
        title="返回"
        content="领取记录管理"
        @back="this.$router.back()"
        class="pageHeader"
    />
    <el-divider></el-divider>
    <el-form :inline="true" :model="formInline" class="form-inline">
      <el-form-item label="领取人">
        <el-input v-model="formInline.people" placeholder="领取人"> </el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="query"
        >查询&nbsp;<el-icon><search /></el-icon
        ></el-button>
        <el-button @click="clearAll"
        >清空查询&nbsp; <el-icon><delete /></el-icon
        ></el-button>
      </el-form-item>
    </el-form>
    <el-divider></el-divider>
    <el-row style="margin-bottom: 10px">
      <el-col :span="2"><el-button type="primary" @click="add">领取教材
        <el-icon><plus /></el-icon>
      </el-button>
      </el-col>
    </el-row>
    <!-- 表格数据-->
    <el-table
        ref="multipleTableRef"
        :data="detail"
        style="width: 100%; text-align: center"
        stripe
        border
        @selection-change="selectChange"
    >
      <el-table-column property="id" label="id" align="center"></el-table-column>
      <el-table-column
          property="book.bookName"
          label="教材名称"
          min-width="120"
          align="center"
      >
      </el-table-column>
      <el-table-column
          property="book.author"
          label="教材作者"
          min-width="80"
          align="center"
      >
      </el-table-column>
      <el-table-column property="count" label="领取数量" align="center" />
      <el-table-column property="people" label="领取人" align="center" />
      <el-table-column
          property="createTime"
          label="领取时间"
          align="center"
          show-overflow-tooltip
      >
        <template #default="scope">
          <div style="display: flex; align-items: center">
            <el-icon><timer /></el-icon>
            <span style="margin-left: 10px">{{
                scope.row.createTime?.replace("T", " ")
              }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column property="content" label="领取说明" align="center" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-popconfirm
              confirm-button-text="是的"
              cancel-button-text="不"
              icon-color="red"
              title="确定删除当前领取记录数据?"
              @confirm="handleDelete(scope.row.id)"
          >
            <template #reference>
              <el-button type="danger"
              >删除<el-icon><delete /></el-icon
              ></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-divider></el-divider>
    <el-row :gutter="20">
      <el-col :span="4"></el-col>
      <el-col :span="16" style="text-align: center"
      ><el-pagination
          background
          layout=" total,prev,pager,  next, jumper,->"
          :total="total"
          v-model:currentPage="currentPage"
          v-model:page-size="pageSize"
          @size-change="handleChange"
          @current-change="handleChange"
      >
      </el-pagination
      ></el-col>
      <el-col :span="4"></el-col>
    </el-row>

    <!-- 对话框-->
    <el-dialog
        v-model="dialogFormVisible"
        :title="editIndex !== '' ? '编辑信息' : '领取教材'"
        @close="dialog = {}"
    >
      <el-form :model="dialog" label-width="120px">
        <el-form-item label="教材" required>
          <el-select v-model="dialog.bookId" placeholder="选择教材" size="large">
            <el-option
                v-for="item in bookList"
                :key="item.id"
                :label="item.bookName"
                :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="领取数量">
          <el-input-number v-model="dialog.count" :min="0" />
        </el-form-item>
        <el-form-item label="作者" required>
          <el-input v-model="dialog.people" placeholder="领取人"></el-input>
        </el-form-item>
        <el-form-item label="领取说明" required>
          <el-input v-model="dialog.content" placeholder="领取说明"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="dialogConfirm">
            <span v-if="editIndex !== ''">修改</span>
            <span v-else>添加</span></el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref } from "vue";
import { list, del, deleteAll, query,add } from "@/api/pickup";
import { ElMessage } from "element-plus";
import {all as book} from "@/api/book";
export default {
  name: "Pickup",
  created() {
    this.handleChange();
    book().then((res) => {
      this.bookList = res.data.data;
    });
  },
  data() {
    return {
      dialog: {},
      dialogFormVisible: false,
      editIndex: "",
      deleteMap: [],
      currentPage: 1,
      pageSize: 6,
      total: 10,
    };
  },
  setup() {
    const detail = ref();
    //分类数据
    const bookList = ref([]);
    const formInline = ref({
      people: "",
    });
    const multipleTableRef = ref();
    return {
      formInline,
      multipleTableRef,
      detail,
      bookList
    };
  },

  methods: {
    query() {
      query(this.formInline)
          .then((res) => {
            const data = res.data.data;
            this.detail = data;
            this.total = data.length;
            ElMessage({
              message: "查询成功",
              type: "success",
            });
          })
          .then((e) => {
            console.log(e);
          });
    },

    handleDelete(id) {
      //删除单个数据
      del(id)
          .then(() => {
            ElMessage({
              message: "删除成功",
              type: "success",
            });
            this.handleChange();
          })
          .catch((e) => {
            console.log(e);
          });
    },

    add() {
      this.dialogFormVisible = true;
    },
    //对话框，领取或者编辑
    dialogConfirm() {
      if (this.editIndex !== "") {
        update(this.dialog)
            .then(() => {
              this.detail[this.editIndex] = JSON.parse(JSON.stringify(this.dialog));
              this.editIndex = "";
              this.dialogFormVisible = false;
              ElMessage({
                message: "修改成功",
                type: "success",
              });
            })
            .catch((e) => {
              ElMessage({
                message: "修改失败！" + e,
                type: "warning",
              });
            });
      } else {
        //领取教材
        console.log("领取教材")
        add(this.dialog)
            .then(() => {
              this.handleChange();
              this.editIndex = "";
              this.dialogFormVisible = false;
              ElMessage({
                message: "领取成功",
                type: "success",
              });
            })
            .catch((e) => {
              ElMessage({
                message: "领取失败！" + e,
                type: "warning",
              });
            });
      }
    },
    clearAll() {
      //清除所有查询
      this.formInline.bookname = "";
      this.formInline.agent = "";
      this.handleChange();
    },
    //删除所有选中
    deleteAll() {
      const ids = [];
      this.deleteMap.forEach((element) => {
        ids.push(element.id);
      });
      //删除所有
      deleteAll(ids)
          .then((res) => {
            ElMessage({
              message: "删除成功",
              type: "success",
            });
            this.$refs.multipleTableRef.clearSelection();
            this.handleChange();
          })
          .then((e) => {
            console.log(e);
          });
    },
    selectChange(row) {
      this.deleteMap = row;
    },
    //分页处理
    handleChange() {
      list(this.currentPage, this.pageSize)
          .then((res) => {
            const data = res.data.data;
            this.total = data.total;
            this.currentPage = data.current;
            this.pageSize = data.size;
            this.detail = data.records;
          })
          .catch((err) => console.log(err));
    },
  },
};
</script>

<style lang="scss" scoped>
.form-inline {
  justify-content: flex-start;
}
.el-table {
  text-align: center;
}
</style>
